<template>
    <page-header-wrapper>
        <a-card :bordered="false">
            <div class="table-page-search-wrapper">
                <r-filter :positions="positions" :members="members" :loading="loading" @query="query"/>
                <s-table :loading="loading" :cols="cols" :data="data" :actions="acts" :selection="true"
                         :operators="operators" :pagination="pagination" @change="change"/>
                <IForm :visible="modal.visible1" :loading="modal.loading" @ok="interviewOk"
                       @cancel="modal.visible1 = modal.loading = false"/>
                <SForm :visible="modal.visible2" :loading="modal.loading" @ok="signOk"
                       @cancel="modal.visible2 = modal.loading = false"/>
            </div>
        </a-card>
    </page-header-wrapper>
</template>

<script>
    import STable from '@/components/Table/PagerTable'
    import RFilter from '@/components/Filter/ResumeFilter'
    import SForm from '@/components/Forms/SignForm'
    import IForm from '@/components/Forms/InterviewForm'
    import API from "@/api"
    import {Modal} from "ant-design-vue"
    import Adapter from "@/utils/adapter"
    import ChangeStatus from "@/utils/statusUtils"

    export default {
        components: {
            STable,
            RFilter,
            SForm,
            IForm
        },
        data() {
            return {
                positionMap: {},
                memberMap: {},
                positions: [],
                members: [],

                loading: true,
                data: [],
                cols: [{title: '编号', key: 'id'}, {title: '岗位', key: 'position'}, {title: '姓名', key: 'name'},
                    {title: '电话', key: 'phone'}, {title: '通过/评价', key: 'rate'},
                    {title: '面试状态', key: 'interviewStatusText'}, {title: '面试时间', key: 'interviewTime'},
                    {title: '面试操作人', key: 'interviewHandler'},
                    {title: '签约状态', key: 'signStatusText'}, {title: '签约时间', key: 'signTime'},
                    {title: '签约操作人', key: 'signHandler'}, {title: '创建时间', key: 'createTime'}],
                acts: [{func: this.view, title: '查看'}, {func: this.edit, title: '编辑'}, {func: this.del, title: '删除'}],
                operators: [{type: 'primary', icon: 'plus', title: '新建', selection: false, func: this.add},
                    {type: 'default', icon: 'team', title: '面试', selection: true, func: this.interview,
                        items: [{key: 1, title: '安排面试'}, {key: 2, title: '面试通过'}, {key: -1, title: '简历不通过'},
                            {key: -2, title: '面试不通过'}, {key: -3, title: '缺席面试'}]},
                    {type: 'default', icon: 'file-done', title: '签约', selection: true, func: this.sign,
                        items: [{key: 1, title: '安排签约'}, {key: 2, title: '签约完成'},
                            {key: -1, title: '拒绝录用'}, {key: -2, title: '对方拒签'}]},
                    {type: 'link', icon: 'file', title: '模板', selection: false, func: this.module},
                    {type: 'link', icon: 'container', title: '导出结果', selection: false, func: this.exp}],
                pagination: {
                    total: 0,
                    current: 1,
                    pageSize: 10,
                    showSizeChanger: true,
                    showQuickJumper: true
                },
                queryParams: {
                    limit: 10,
                    offset: 0
                },
                modal: {
                    visible1: false,
                    visible2: false,
                    loading: false,
                    keys: null
                }
            }
        },
        mounted() {
            API.User.BaseInfo().then((res) => {
                if (res) {
                    this.members = res.data.members || []
                    this.positions = res.data.positions || []
                    this.memberMap = {}
                    this.positionMap = {}
                    this.members.forEach((item) => this.memberMap[item.id] = item.name)
                    this.positions.forEach((item) => this.positionMap[item.id] = item.name)
                    this.query()
                }
            })
        },
        methods: {
            change() {
                this.queryParams.limit = this.pagination.pageSize
                this.queryParams.offset = (this.pagination.current - 1) * this.pagination.pageSize
                this.query()
            },
            query(params) {
                this.loading = true
                if (params) {
                    params.limit = this.queryParams.limit
                    params.offset = this.queryParams.offset
                    this.queryParams = params
                }
                API.Resume.ListNotEvaluated(this.queryParams).then((res) => {
                    if (res) {
                        this.pagination.total = res.total
                        Adapter.Resume(res.data, this.positionMap, this.memberMap)
                        this.data = res.data
                    }
                }).finally(() => this.loading = false)
            },
            view(record) {
                this.$router.push({name: 'resumeDetails', params: {id: record.id, positionMap: this.positionMap, back: '/system/resume'}})
            },
            module() {
                window.open("/resume.xlsx");
            },
            add() {
                this.$router.push({name: 'resumeModify', params: {positions: this.positions, back: '/system/resume'}})
            },
            edit(record) {
                this.$router.push({name: 'resumeModify', params: {id: record.id, positions: this.positions, back: '/system/resume'}})
            },
            del(record) {
                Modal.confirm({
                    title: '提示',
                    content: '是否确认该简历？',
                    onOk: () => {
                        API.Resume.Del({id: record.id}).then((res) => {
                            if (res) {
                                this.$notification.success({message: '提示', description: '简历已删除'})
                                this.data = this.data.filter(item => item.id !== record.id)
                            }
                        })
                    },
                    onCancel () {}
                })
            },
            interview(opt, keys) {
                if (opt === 1) {
                    this.modal.keys = keys
                    this.modal.visible1 = true
                }
                else ChangeStatus(this.query, 1, opt, keys)
            },
            interviewOk(id) {
                ChangeStatus(this.query, 1, 1, this.modal, id)
            },
            sign(opt, keys) {
                if (opt === 1) {
                    this.modal.keys = keys
                    this.modal.visible2 = true
                }
                else ChangeStatus(this.query, 0, opt, keys)
            },
            signOk(time) {
                ChangeStatus(this.query, 0, 1, this.modal, time)
            },
            exp() {
                //ResumeExport

                API.Resume.ExportResume(this.queryParams).then((res) => {
                    if (res) {
                        let blob = Adapter.ResumeExport(res.data, this.positionMap)
                        if (blob) {
                            let url = URL.createObjectURL(blob);
                            let aLink = document.createElement('a');
                            aLink.href = url;
                            aLink.download = "导出简历.xlsx";
                            let event;
                            if(window.MouseEvent) event = new MouseEvent('click');
                            else
                            {
                                event = document.createEvent('MouseEvents');
                                event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                            }
                            aLink.dispatchEvent(event);
                        }
                    }
                }).finally(() => this.loading = false)
            }
        }
    }
</script>
